package com.synchronous7;

import org.junit.Test;

import java.util.concurrent.Callable;

public class TestLazy{
    @Test
    public void test01() {
        LazyOne s1 = LazyOne.getInstance1();
        LazyOne s2 = LazyOne.getInstance1();
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s1 == s2);
    }

    //    把s1和s2声明在外，是想要在线程的匿名内部类中为s1和s2赋值
    LazyOne s1;
    LazyOne s2;

    @Test
    public void test02() {
        Thread t1 = new Thread() {
            @Override
            public void run() {
                s1 = LazyOne.getInstance2();
            }
        };
        Thread t2 = new Thread() {
            @Override
            public void run() {
                s2 = LazyOne.getInstance2();
            }
        };
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s1 == s2);

    }
    LazySingle obj1;
    LazySingle obj2;
    @Test
    public void test03(){
        Thread t1 = new Thread(){
            @Override
            public void run() {
                obj1 = LazySingle.getInstance();
            }
        };
        Thread t2 = new Thread(){
            @Override
            public void run() {
                obj2 = LazySingle.getInstance();
            }
        };
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(obj1);
        System.out.println(obj2);
        System.out.println(obj1 == obj2);
    }
}
// 实现线程的四种方式：继承Thread类、实现Readable接口、
// 实现Callable接口、通过线程池构建线程

